package com.idbk.chargestation.activity.charge;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.text.Html;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.idbk.chargestation.AppContext;
import com.idbk.chargestation.R;
import com.idbk.chargestation.activity.pile.ActivityReservePileCommit;
import com.idbk.chargestation.adapter.gunDetailAdapter;
import com.idbk.chargestation.base.BaseActivity;
import com.idbk.chargestation.bean.JsonGunInfor;
import com.idbk.chargestation.bean.JsonPointPile;

import java.util.List;

/**充电枪详情页
 * Created by wuz on 2019/1/10.
 */

public class ActivityGunDetail extends BaseActivity implements View.OnClickListener{

    private Context mContext;
    private ListView mListView;
    private JsonPointPile mData;
    private gunDetailAdapter mAdapter;
    private TextView mBook;
    private int num = 0, free = 0;
    private LinearLayout mLinearBook;

    public final static int REQUEST_BOOKING = 376;
    private int userRole;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_gun_detail);

        initData();

        initView();
    }

    private void initData(){
        Bundle bundle = getIntent().getExtras();
        mData = bundle.getParcelable("gunInfor");
        num = mData.gunInfos.size();
        for (int i = 0; i < num; i++) {
            if (mData.gunInfos.get(i).gunStatus == 3) {
                free++;
            }
        }
    }

    private void initView(){
        mContext = ActivityGunDetail.this;
        setupToolBar2();// 显示标题栏

        mLinearBook = (LinearLayout) findViewById(R.id.linear_book);
        mBook = (TextView) findViewById(R.id.textview_book);
        mBook.setOnClickListener(this);

        userRole = AppContext.getInstance().getUserRole();
        if (userRole == 2){
            mLinearBook.setVisibility(View.GONE);
        }else {
            mLinearBook.setVisibility(View.VISIBLE);
        }
        mListView = (ListView) findViewById(R.id.gun_list);
        mAdapter = new gunDetailAdapter(mContext, mData);
        mListView.setAdapter(mAdapter);
        setGunSort(mData.gunInfos);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int num = mData.gunInfos.get(position).gunNum;
                String str = mData.gunInfor(num);
                new AlertDialog.Builder(mContext)
                        .setMessage(Html.fromHtml(str))
                        .setCancelable(true)
                        .setNegativeButton(R.string.common_ok, null)
                        .create()
                        .show();
            }
        });
    }

    private void setGunSort(List<JsonGunInfor> list){
        int num1, num2;
        JsonGunInfor sign;
        for(int i = 0; i < list.size()-1; i++){
            for (int j = i+1; j < list.size(); j++){
                num1 = list.get(i).gunNum;
                num2 = list.get(j).gunNum;
                if (num2 < num1){
                    sign = list.get(i);
                    list.set(i, list.get(j));
                    list.set(j, sign);
                }
            }
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.textview_book) {
            reservePile();
        }
    }

    private void reservePile() {
        if (free > 0 && mData.bookAvailable) {
            Intent intent = new Intent(this, ActivityReservePileCommit.class);
            intent.putExtra(JsonPointPile.KEY, mData);
            startActivityForResult(intent, REQUEST_BOOKING);
        } else {
            Snackbar.make(mBook, "当前没有可预约的电桩", Snackbar.LENGTH_LONG).show();
        }

    }

}
